/*************************************************************************
 * File Name:    Rotate_Image.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/16 16:55:40
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Rotate Image
 |
 | You are given an n x n 2D matrix representing an image.
 |
 | Rotate the image by 90 degrees (clockwise).
 |
 | Follow up:
 | Could you do this in-place?
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    void rotate(vector<vector<int> > &matrix)
    {
        int n = matrix.size();
        for (int i = 0; i < n / 2; ++i) {
            for (int j = i; j < n - 1 - i; ++j) {
                rotate_place(matrix, i, j);
            }
        }
    }

private:
    void rotate_place(vector<vector<int> > &matrix, int i, int j)
    {
        int n = matrix.size();

        int t = matrix[i][j];
        matrix[i][j] = matrix[n - 1 - j][i];
        matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
        matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
        matrix[j][n - 1 - i] = t;
    }
};

